package protocoltcp;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NettyServer {
    public static void main(String[] args) {
        //创建2个线程组  bossGroup 和 workerGroup  运行时无限循环
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);//处理连接请求
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();//业务处理
        //创建启动对象
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();//可以设置启动参数
            bootstrap.group(bossGroup,workerGroup)//设置2个线程组
                    .channel(NioServerSocketChannel.class)//设置通道实现类型
                    .childHandler(new MyServerInitializer());//设置对应的管道处理器
            System.out.println("服务器准备好了");
            //绑定端口并同步
            ChannelFuture sync = bootstrap.bind(6668).sync();
            //对关闭通道进行监听
            sync.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}
